package com.engine.jz.controller;

import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.jz.service.JzjgService;
import com.engine.jz.service.impl.JzjgServiceImpl;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @version 1.0
 * @Title ecology-9
 * @Company 泛微软件
 * @CreateDate 2024/11/27
 * @Description 天波
 * @Author AdminZm
 */
public class TelPoController {

    public JzjgService getJzjgService(User user) {
        return ServiceUtil.getService(JzjgServiceImpl.class, user);
    }

    /**
     * 设备连接推送
     */
    @POST
    @Path("/connect")
    @Produces(MediaType.APPLICATION_JSON)
    public String connect(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        new BaseBean().writeLog("connect start.");
        Map<String, Object> params = ParamUtil.request2Map(request);
        new BaseBean().writeLog("connect params:" + params);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("errCode", "00");
        return new Gson().toJson(resultMap);
    }

    /**
     * 设备断开推送
     */
    @POST
    @Path("/disconnect")
    @Produces(MediaType.APPLICATION_JSON)
    public String disconnect(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        new BaseBean().writeLog("disconnect start.");
        Map<String, Object> params = ParamUtil.request2Map(request);
        new BaseBean().writeLog("disconnect params:" + params);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("errCode", "00");
        return new Gson().toJson(resultMap);
    }

    /**
     * 推送查询记录信息
     */
    @POST
    @Path("/EventRecordsOnline")
    @Produces(MediaType.APPLICATION_FORM_URLENCODED)
    public String handlePushData(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        BaseBean baseBean = new BaseBean();
        baseBean.writeLog("handlePushData go.");
        Map<String, Object> resultMap = new HashMap<>();
        try {
            Map parameterMap = request2Map(request);
            if (CollectionUtils.isEmpty(parameterMap)) {
                resultMap.put("errCode", "00");
                return new Gson().toJson(resultMap);
            }
//            baseBean.writeLog("handlePushData parameterMap:" + parameterMap);
            String parameterStr = parameterMap.toString();
            if (StringUtils.isEmpty(parameterStr) || parameterStr.length() <= 2) {
                resultMap.put("errCode", "00");
                return new Gson().toJson(resultMap);
            }
            parameterStr = parameterStr.substring(1, parameterStr.length() - 1);
            Gson gson = new Gson();
            Type type = new TypeToken<Map<String, Object>>() {
            }.getType();
            Map<String, Object> params = gson.fromJson(parameterStr, type);

            User user = new User();
            user.setUid(1);
            baseBean.writeLog("handlePushData go to handlePushData.");
            resultMap = getJzjgService(user).handlePushData(params);
        } catch (Exception e) {
            baseBean.writeLog("handlePushData error:" + e);
            resultMap.put("errCode", "00");
        }
        return new Gson().toJson(resultMap);
    }

    /**
     * 推送查询记录信息
     */
    @POST
    @Path("/EventRecords")
    @Produces(MediaType.TEXT_PLAIN)
    public String handlePushDataTemp(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        BaseBean baseBean = new BaseBean();
        baseBean.writeLog("handlePushDataTemp go.");
        Map<String, Object> resultMap = new HashMap<>();
        try {
            Map parameterMap = request2Map(request);
            if (CollectionUtils.isEmpty(parameterMap)) {
                resultMap.put("errCode", "00");
                return new Gson().toJson(resultMap);
            }
//            baseBean.writeLog("handlePushDataTemp parameterMap:" + parameterMap);
            String parameterStr = parameterMap.toString();
            if (StringUtils.isEmpty(parameterStr) || parameterStr.length() <= 2) {
                resultMap.put("errCode", "00");
                return new Gson().toJson(resultMap);
            }
//            baseBean.writeLog("handlePushDataTemp parameterStr:" + parameterStr);
            parameterStr = parameterStr.substring(1, parameterStr.length() - 1);
            Gson gson = new Gson();
            Type type = new TypeToken<Map<String, Object>>() {
            }.getType();
            Map<String, Object> params = gson.fromJson(parameterStr, type);
//            baseBean.writeLog("handlePushDataTemp go to handlePushData:" + params);
            User user = new User();
            user.setUid(1);
            resultMap = getJzjgService(user).handleNowPushData(params);

        } catch (Exception e) {
            baseBean.writeLog("handlePushDataTemp error:" + e);
            resultMap.put("errCode", "00");
        }
        resultMap.put("errCode", "00");
        return new Gson().toJson(resultMap);
    }

    public static Map<String, Object> request2Map(HttpServletRequest var0) {
        Map var1 = var0.getParameterMap();
        HashMap var2 = new HashMap();
        Iterator var3 = var1.entrySet().iterator();
        String var5 = "";

        for(Object var6 = null; var3.hasNext(); var2.put(var5, var6)) {
            Map.Entry var4 = (Map.Entry)var3.next();
            var5 = (String)var4.getKey();
            Object var7 = var4.getValue();
            if (null == var7) {
                var6 = null;
            } else if (var7 instanceof String[]) {
                String[] var8 = (String[])((String[])var7);
                if (var8.length == 1) {
                    var6 = var8[0];
                } else {
                    var6 = var8;
                }
            } else {
                var6 = var7.toString();
            }
        }
        return var2;
    }

    /**
     * 上报任务结果处理
     */
    @POST
    @Path("/pushTaskResult")
    @Produces(MediaType.APPLICATION_JSON)
    public String pushTaskResult(@Context HttpServletRequest request, @Context HttpServletResponse response) {
//        User user = HrmUserVarify.getUser(request, response);
        Map<String, Object> params = ParamUtil.request2Map(request);
        User user = new User();
        user.setUid(1);
        Map<String, Object> resultMap = getJzjgService(user).pushTaskResult(params);
        return new Gson().toJson(resultMap);
    }

    /**
     * 推送指定人员去对应设备
     */
    @POST
    @Path("/pushHrmToDevice")
    @Produces(MediaType.APPLICATION_JSON)
    public String pushHrmToDevice(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> params = ParamUtil.request2Map(request);
        User user = new User();
        user.setUid(1);
        Map<String, Object> resultMap = getJzjgService(user).pushHrmToDevice(params);
        return new Gson().toJson(resultMap);
    }

    /**
     * 接收处理推送查询人员信息
     */
    @POST
    @Path("/pushUsersOnline")
    @Produces(MediaType.APPLICATION_FORM_URLENCODED)
    public String pushUsersOnline(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        BaseBean baseBean = new BaseBean();
        baseBean.writeLog("pushUsersOnline go.");
        Map<String, Object> resultMap = new HashMap<>();
        try {
            Map parameterMap = request2Map(request);
            if (CollectionUtils.isEmpty(parameterMap)) {
                resultMap.put("errCode", "00");
                return new Gson().toJson(resultMap);
            }
            baseBean.writeLog("pushUsersOnline parameterMap:" + parameterMap);
            String parameterStr = parameterMap.toString();
            if (StringUtils.isEmpty(parameterStr) || parameterStr.length() <= 2) {
                resultMap.put("errCode", "00");
                return new Gson().toJson(resultMap);
            }
            parameterStr = parameterStr.substring(1, parameterStr.length() - 1);
            Gson gson = new Gson();
            Type type = new TypeToken<Map<String, Object>>() {
            }.getType();
            Map<String, Object> params = gson.fromJson(parameterStr, type);

            User user = new User();
            user.setUid(1);
            baseBean.writeLog("pushUsersOnline go to handlePushData.");
            resultMap = getJzjgService(user).handlePushUsersData(params);
        } catch (Exception e) {
            baseBean.writeLog("pushUsersOnline error:" + e);
            resultMap.put("errCode", "00");
        }
        return new Gson().toJson(resultMap);
    }
}
